﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Castle.Windsor;
using Castle.Windsor.Configuration.Interpreters;
using Castle.Core.Resource;

using Light.Framework.Contract;

namespace Light.Framework.Castle
{
    /// <summary>
    /// 基于Castle的组件工厂
    /// </summary>
    public class ComponentFactory : IFactory
    {
        private static IWindsorContainer _container;

        public ComponentFactory()
        {
            if (_container == null)
            {
                lock (this)
                {
                    if (_container == null)
                    {
                        _container = new WindsorContainer(new XmlInterpreter(new ConfigResource()));
                    }
                }
            }
        }

        public T Create<T>()
        {
            if (_container != null)
                return _container.Resolve<T>();
            else
                return default(T);
        }

        public T Create<T>(string key)
        {
            if (_container != null)
                return _container.Resolve<T>(key);
            else
                return default(T);
        }

        public object Create(Type type)
        {
            if (_container != null)
                return _container.Resolve(type);
            else
                return null;
        }

        public void Dispose()
        {
            if (_container != null)
                _container.Dispose();
        }
    }
}
